const fs = require('fs'),
  path = require('path')

const pagePath = path.join(__dirname, '../src/pages')
const publicPath = path.join(__dirname, '../public/html')
const pageNames = fs.readdirSync(pagePath)
const publicNames = fs.readdirSync(publicPath)

const htmlText = `<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum=1,user-scalable=no,viewport-fit=cover">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title>mirror-admin</title>
  </head>
  <body>
    <noscript>
      <strong>很抱歉，系统在没有JavaScript的情况下不能正常工作。请启用它再继续。</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>`

// 去掉扩展名
let publicNamesFormat = []
for (let pub of publicNames) {
  publicNamesFormat.push(pub.split('.')[0])
}
let pages = {}

for (let page of pageNames) {
  // 生成对应的html文件
  if (!publicNamesFormat.includes(page)) {
    const fn = `${publicPath}/${page}.html`
    fs.writeFileSync(fn, htmlText)
  }
  // 生成pages对象
  pages[page] = {
    entry: `src/pages/${page}/main.js`,
    template: `public/html/${page}.html`,
    filename: `${page}.html`
  }
}
module.exports = pages
